package com.xt.purchasingsystem.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.xt.purchasingsystem.dto.BuyerExecuteDto;
import com.xt.purchasingsystem.mapper.BuyerExecuteDetailMapper;
import com.xt.purchasingsystem.mapper.BuyerExecuteMapper;
import com.xt.purchasingsystem.mapper.BuyerPlanDetailMapper;
import com.xt.purchasingsystem.model.BuyerExecute;
import com.xt.purchasingsystem.model.BuyerExecuteDetail;
import com.xt.purchasingsystem.model.BuyerPlanDetail;
import com.xt.purchasingsystem.service.BuyerExecuteService;
import com.xt.purchasingsystem.utils.IdWorker;
import com.xt.purchasingsystem.utils.Result;
import com.xt.purchasingsystem.utils.ResultFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.stream.Collectors;

/**
 * <p>
 * 采购执行单业务实现类
 * </p>
 *
 * @author yhp
 * @since 2020-05-31
 */
@Service
@Transactional
public class BuyerExecuteServiceImpl extends ServiceImpl<BuyerExecuteMapper, BuyerExecute> implements BuyerExecuteService {
    @Autowired
    private IdWorker idWorker;
    @Autowired
    private BuyerExecuteMapper buyerExecuteMapper;
    @Autowired
    private BuyerExecuteDetailMapper buyerExecuteDetailMapper;
    @Autowired
    private ThreadPoolExecutor threadPoolExecutor;
    @Autowired
    private BuyerPlanDetailMapper buyerPlanDetailMapper;

    @Override
    public Result saveBuyerExecute(BuyerExecuteDto buyerExecuteDto) {
        String id = String.valueOf(idWorker.nextId());
        buyerExecuteDto.getBuyerExecute().setId(id);
        buyerExecuteMapper.insert(buyerExecuteDto.getBuyerExecute());
        List<BuyerExecuteDetail> collect = buyerExecuteDto.getBuyerExecuteDetails().stream().filter(d -> d.getAmount() != null && d.getAmount() > 0).collect(Collectors.toList());
        for (BuyerExecuteDetail buyerExecuteDetail : collect) {
            buyerExecuteDetail.setId(String.valueOf(idWorker.nextId()));
            buyerExecuteDetail.setBuyerExecuteId(id);
            buyerExecuteDetailMapper.insert(buyerExecuteDetail);
        }
        return ResultFactory.buildSuccessResult("添加成功,需要审核!");
    }

    @Override
    public Result getAllBuyerExecuteNoCheck(Integer currNo, Integer pageSize) {
        PageHelper.startPage(currNo, pageSize);
        List<BuyerExecuteDto> allBuyerExecuteNoCheck = buyerExecuteMapper.getAllBuyerExecuteNoCheck();
        PageInfo<BuyerExecuteDto> pageInfo = new PageInfo(allBuyerExecuteNoCheck);
        return ResultFactory.buildSuccessResult("查询成功", pageInfo);
    }

    @Override
    public Result saveBuyerExecuteAndUpdateBuyerPlanDetailExecuteTag(BuyerExecuteDto buyerExecuteDto) {
        String id = String.valueOf(idWorker.nextId());
        buyerExecuteDto.getBuyerExecute().setId(id);
        CompletableFuture.runAsync(() -> {
            buyerExecuteMapper.insert(buyerExecuteDto.getBuyerExecute());
        }, threadPoolExecutor);
        List<BuyerExecuteDetail> collect = buyerExecuteDto.getBuyerExecuteDetails().stream().filter(d -> d.getAmount() != null && d.getAmount() > 0).collect(Collectors.toList());
        CompletableFuture.runAsync(() -> {
            for (BuyerExecuteDetail buyerExecuteDetail : collect) {
                buyerExecuteDetail.setId(String.valueOf(idWorker.nextId()));
                buyerExecuteDetail.setBuyerExecuteId(id);
                buyerExecuteDetailMapper.insert(buyerExecuteDetail);
            }
        }, threadPoolExecutor);
        CompletableFuture.runAsync(() -> {
            for (BuyerPlanDetail buyerPlanDetailId : buyerExecuteDto.getBuyerPlanDetailIds()) {
                buyerPlanDetailId.setExecuteTag("E001-1");
                buyerPlanDetailMapper.updateById(buyerPlanDetailId);
            }
        }, threadPoolExecutor);
        return ResultFactory.buildSuccessResult("添加成功,需要审核!");
    }

    @Override
    public Result getAllBuyerExecuteIsCheck(Integer currNo, Integer pageSize) {
        PageHelper.startPage(currNo, pageSize);
        List<BuyerExecuteDto> allBuyerExecuteNoCheck = buyerExecuteMapper.getAllBuyerExecuteIsCheck();
        PageInfo<BuyerExecuteDto> pageInfo = new PageInfo(allBuyerExecuteNoCheck);
        return ResultFactory.buildSuccessResult("查询成功", pageInfo);
    }


}
